﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace ReversiFrontend
{
    public enum HighlightMode 
    {
        // während des Spiels:
        ActivePlayer,   // aktueller Spieler
        NextPlayer,     // nächster Spieler
        // nach dem Spiel:
        Winner,         // gewonnen
        Loser,          // verloren
        Neutral,        // Unentschieden
        // default
        Unknown         // ungewiss      
    }
    public partial class PlayerInfo : UserControl
    {
        private bool isHuman = true;
        public bool IsHuman
        {
            get
            {
                return isHuman;
            }
            set
            {
                if (value != isHuman)
                {
                    isHuman = value;
                    iconBox.Image = isHuman ? Properties.Resources.human : Properties.Resources.ai;
                }
            }
        }

        private string playerName = "name";
        /// <summary>
        /// Gets or sets the name of the player.
        /// </summary>
        /// <value>
        /// The name of the player.
        /// </value>
        public string PlayerName
        {
            get
            {
                return playerName;
            }
            set
            {
                if (value != playerName)
                {
                    playerName = value;
                    UpdateText();
                }
            }
        }

        private int count = 0;
        public int Count
        {
            get
            {
                return count;
            }
            set
            {
                if (value != count)
                {
                    count = value;
                    UpdateText();
                }
            }
        }

        private HighlightMode highlight = HighlightMode.Unknown;
        public HighlightMode Highlight
        {
            get
            {
                return highlight;
            }
            set
            {
                if (value != highlight)
                {
                    highlight = value;
                    BorderStyle = System.Windows.Forms.BorderStyle.None;
                    BackColor = Color.Transparent;
                    switch (highlight){
                        case HighlightMode.ActivePlayer:
                            BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
                            break;
                        
                        case HighlightMode.NextPlayer:
                            // kein besonderer style
                            break;
                        case HighlightMode.Winner:
                            BackColor = Color.Green;
                            break;
                        case HighlightMode.Loser:
                            BackColor = Color.Red;
                            break;
                        case HighlightMode.Neutral:
                        case HighlightMode.Unknown:
                            // kein besonderer style
                            break;
                    }
                }
            }
        }

        private void UpdateText()
        {
            label.Text = String.Format("{0}: {1}", PlayerName, Count);
            // kleinere Schrifftgroeße fuer lange namen
            // -> text mit aktueller schrifftgröße rendern, ergebnis mit erlaubter größe
            // vergleichen und schrifftgröße im entsprechenden verhältnis anpassen
            float height = label.Height * 0.99f - 5;
            float width = label.Width * 0.99f - 10;

            label.SuspendLayout();

            Font tryFont = label.Font;
            Size tempSize = TextRenderer.MeasureText(label.Text, tryFont);

            float heightRatio = height / tempSize.Height;
            float widthRatio = width / tempSize.Width;
            // maximal schrifftgröße 10 benutzen
            tryFont = new Font(tryFont.FontFamily, Math.Min(10, tryFont.Size * Math.Min(widthRatio, heightRatio)), tryFont.Style);
          
            label.Font = tryFont;
            label.ResumeLayout();

        }

        public PlayerInfo()
        {
            InitializeComponent();
        }


        #region Serialization stuff
        public void DumpData(Dictionary<string, object> data, string suffix="")
        {
            data.Add("highlightMode"+suffix, Highlight);
            data.Add("isHuman" + suffix, isHuman);
            data.Add("name" + suffix, PlayerName);
            data.Add("count" + suffix, Count);

        }

        public void LoadData(Dictionary<string, object> data, string suffix = "")
        {

            Highlight = (HighlightMode)data["highlightMode" + suffix];
            isHuman = (bool)data["isHuman" + suffix];
            PlayerName = (string)data["name"+suffix];
            Count = (int)data["count" + suffix];
        }
        #endregion
    }
}
